<template>
	<view class="">
		<u-navbar placeholder autoBack bgColor="transparent"></u-navbar>
		<view class="usertop flex align-center justify-center">
			<u-image src="/static/logo.png" width="168rpx" height="188rpx"></u-image>
		</view>

		<view class="mt-2 mx-4" style="height: 80rpx;">
			<u-input v-model="account" placeholder="请输入手机号" placeholderStyle="fontSize:30rpx" shape="circle"
				:customStyle="{height:'80rpx',background:'#F5F5F5'}"></u-input>
		</view>
		<view class="flex align-center justify-center mt-5 mx-4" style="height: 80rpx;">
			<u-input v-model="code" shape="circle" placeholderStyle="fontSize:30rpx" :customStyle="{height:'80rpx',background:'#F5F5F5'}"
				placeholder="请输入验证码" clearable>
				<template slot="suffix">
					<u-code ref="uCode" @change="codeChange" seconds="20" changeText="X秒重新获取"></u-code>
					<view class="font-28" style="color: #04BF79;" @click="getCode">{{tips}}</view>
				</template>
		
			</u-input>
		</view>
		
		<view class="mt-5 mx-4" style="height: 80rpx;">
			<u-input v-model="password" placeholder="请设置登录密码" placeholderStyle="fontSize:30rpx" shape="circle"
				:customStyle="{height:'80rpx',background:'#F5F5F5'}"></u-input>
		</view>
		
		<view class="mt-5 mx-4" style="height: 80rpx;">
			<u-input :readonly="isShare" v-model="referrer" placeholder="请输入邀请人的邀请码(没有可不填写)" placeholderStyle="fontSize:30rpx" shape="circle"
				:customStyle="{height:'80rpx',background:'#F5F5F5'}"></u-input>
		</view>


		<commonButton title="注册" :background="isable?'#04BF79':'#C3FFC3'" @click="submit"></commonButton>
		

		<view class="flex align-center justify-center" style="margin-top: 180rpx;">
			<u-image :src="isagree?'/static/select.png':'/static/noselect.png'" width="20" height="20"
				@click="isagree=!isagree"></u-image>
			<view class="ml-1 font-24" style="color:#BDBDBD;">
				我已阅读并同意<text style="color: #3289FF;" @click="$u.route('pages/login/agreement',{type:'privacy'})">《隐私政策》</text>和<text style="color: #3289FF;" @click="$u.route('pages/login/agreement',{type:'user'})">《用户协议》</text>
			</view>
		</view>




	</view>
</template>

<script>
	export default {
		data() {
			return {
				account: '',
				password: '',
				code: '',
				tips: '',
				openid: '',
				isagree: false,
				referrer:'',
				isShare:false
			}
		},
		computed: {
			isable() {
				if (!uni.$u.test.isEmpty(this.account) && !uni.$u.test.isEmpty(this.password) && !uni.$u.test.isEmpty(this.code) && this.isagree) {
					return true
				} else {
					return false
				}

			}
		},
		onLoad(e) {
			if (e.openid) {
				this.openid = e.openid
			} else {
				uni.login({
					provider: 'weixin',
					success: (wxres) => {
						this.$api.wxInfo({
							code: wxres.code
						}).then(res => {
							this.openid = res.data.openid
						})
					}
				})
				this.referrer = e.code
				this.isShare = uni.$u.test.isEmpty(e.code)?false:true
			}
		
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						let parmar = {
							mobile:this.account,
							event:'reg'
						}
						this.$api.verification(parmar).then(res=>{
							uni.hideLoading();
							if(res.code === 1){
								// 这里此提示会被this.start()方法中的提示覆盖
								uni.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();
							}
						})	
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			submit() {
				let parmar = {
					mobile:this.account,
					code:this.code,
					password:this.password,
					referrer:this.referrer,
					openid:this.openid
				}
				this.$api.register(parmar).then(res=>{
					if(res.code === 1){
						this.$store.commit('user/setUserInfo', res.data.userinfo)
						this.$store.commit('user/setToken', res.data.userinfo.token)
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 1000)
					}else{
						uni.login({
							provider:'weixin',
							success: (reswx) => {		
								this.$api.wxInfo({
									code:reswx.code
								}).then(res=>{
									if(res.code === 1){
										this.$api.login({openid:res.data.openid}).then(result=>{
											if (result.code == 1) {
												uni.hideLoading()
												this.$store.commit('user/setUserInfo', result.data.userinfo)
												this.$store.commit('user/setToken', result.data.userinfo.token)
												uni.showToast({
													title: result.msg,
													icon: 'none'
												});
												setTimeout(() => {
													uni.switchTab({
														url: '/pages/index/index'
													})
												}, 1000)
																				
											}	
										})
									}
								})		 
							}
						})	
						
						
					}
					
				})

			},
			
		}
	}
</script>

<style>
	.usertop {
		/* background: url(@/static/bg/login.png) no-repeat; */
		background-size: 100% 272rpx;
		height: 272rpx;
	}
</style>